<?php
namespace Admin\Controller;

class VisitController extends BaseController{
    public function index(){
    	$Visit = D('Visit');
    	$object = $Visit->all($this->page($Visit->total()));
    	if ($object){
	    	import('Yvjie.QQWry');
	    	$QQWry = \QQWry::getInstance();
	    	foreach ($object as $key=>$value){
	    		$object[$key]['ip'] = $value['ip'].'<br>'.$QQWry->getAddr($value['ip']);
	    	}
    	}
    	$this->assign('All',$object);
    	$this->display();
    }
    
    public function output(){
		if (IS_POST){
			$output = '"IP","访问页面","当日次数","第一次","最后一次",';
			$Visit = D('Visit');
			$object = $Visit->all2();
			if ($object){
				import('Yvjie.QQWry');
				$QQWry = \QQWry::getInstance();
				foreach ($object as $value){
					$output .= "\r\n".'"'.$value['ip'].' -- '.$QQWry->getAddr($value['ip']).'","'.htmlspecialchars_decode($value['url']).'","'.$value['count'].'","'.dateFormat($value['date1']).'","'.dateFormat($value['date2']).'",';
				}
			}
			$output = mb_convert_encoding($output,'GBK','UTF-8');
			$file = C('OUTPUT_DIR').'visit_'.date('YmdHis').'.csv';
			if (file_put_contents(ROOT_PATH.'/'.$file,$output)){
				$Visit->truncate();
				$this->success(NULL,'文件生成成功！<a href="'.C('WEB_URL').$file.'">下载</a> <a href="javascript:;" onclick="addTab(\''.U('Output/index').'\',\'导出的数据\')">去管理文件</a> <a href="'.U(CONTROLLER_NAME.'/index').'">返回</a>',0,2);
			}else{
				$this->failed('文件生成失败，请检查'.C('OUTPUT_DIR').'目录权限！');
			}
		}
		$this->confirm('确定要将数据导出到文件并清空数据表吗？');
    }
    
    public function js(){
    	$js = ROOT_PATH.'/Public/Home/js/Visit.js';
    	file_put_contents($js,preg_replace('/url : \'[\w\W]*\',/U','url : \''.C('WEB_URL').'index.php'.(C('URL_MODEL') ? '/Public/visit.html' : '?c=Public&a=visit').'\',',file_get_contents($js))) ? $this->success(C('PREV_URL'),'配置更新成功！') : $this->failed('配置更新失败，请检查Public/Home/js目录权限！');
    }
}